package com.los;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: java8
 * @description: stream实战
 * @author: Yujuan
 * @create: 2018-06-14 15:11
 **/
public class StreamInAction {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        //筛选出2011年的交易，从小到大排序
        transactions.stream().filter(transaction -> transaction.getYear()==2011)
                .sorted(Comparator.comparing(Transaction::getValue))
                .forEach(System.out::println);

        transactions.stream().map(transaction -> transaction.getTrader().getCity()).forEach(System.out::println);


        //有没有在米兰的
        transactions.stream().anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));

        //交易最高的
        transactions.stream().map(transaction -> transaction.getValue()).reduce(Integer::max).ifPresent(System.out::println);

        //交易最低的
        transactions.stream().map(transaction -> transaction.getValue()).reduce((t1,t2)->t1<t2?t1:t2).ifPresent(System.out::println);
    }
}
